<%@ page contentType="application/json; charset=UTF-8" pageEncoding="UTF-8" errorPage="/error.jsp"%>
<%@page import="dbexp.model.Sequence"%>
<%@page import="dbexp.model.db.DbUser"%>
<%@page import="dbexp.connection.DbMetaData"%>
<%@page import="dbexp.connection.DbConnection"%>
<%@page import="org.json.JSONObject"%>
<%
DbUser u = (DbUser)session.getAttribute("USER");
String name = request.getParameter("name");
String schema = request.getParameter("schema");
String action = request.getParameter("action");
String min= request.getParameter("min");
String max = request.getParameter("max");
String inc = request.getParameter("inc");
String last = request.getParameter("last");

boolean brak = (name==null || name.trim().equals("null")) && (schema==null || schema.trim().equals("null"));
Sequence ob =  new Sequence(name,schema);
if(min!=null){
	ob.setMinValue(min);
}
if(max!=null){
	ob.setMaxValue(max);
}
if(inc!=null){
	ob.setIncrementBy(Integer.valueOf(inc));
}
if(!brak && action.equals("create")){
	ob.setLastNumber(Integer.valueOf(last));
	int status = DbMetaData.operateObject(u,ob,DbConnection.CREATE);
}else if(action.equals("update")){
	int status = DbMetaData.operateObject(u,ob,DbConnection.ALTER);
}
	ob = DbMetaData.getSequence(u, name, schema);
out.clearBuffer();%><%=new JSONObject().
													put("success", true).
													put("data",new JSONObject().
															put("name", ob.getName()).
															put("schema",ob.getSchema()).
															put("min",ob.getMin()).
															put("max",ob.getMaxValue()).
															put("inc",ob.getIncrementBy()).
															put("last",ob.getLastNumber()))%>